# encoding: utf-8
# @File  : base.py
# @Author: wu shaofan
# @Date  :  2024/06/18
# @Desc :
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from utils.get_dirver import UtilsDriver
from selenium.webdriver.common.by import By


# 对象库层基类封装
class Base:
    def __init__(self):
        self.driver = UtilsDriver().get_app_driver()

    # 查找元素方法 (提供：点击、输入、获取文本)使用
    def base_find_element(self, loc, timeout=30, poll=0.5):
        try:
            ele = WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll).until(EC.presence_of_element_located(loc))
            return ele
        except Exception as e:
            raise e

    # 通过元素文本查找单个元素
    def base_find_element_by_text(self, text, timeout=10):
        """
        通过元素文本查找单个元素
        :param text: 元素文本
        :param timeout: 超时时间，默认为10秒
        :return: 找到的单个元素对象
        """
        loc = (By.XPATH, f'//*[contains(@text,"{text}")]')
        return self.base_find_element(loc, timeout)

    # 点击操作
    def base_click(self, loc):
        WebDriverWait(self.driver, 10, 0.5).until(EC.element_to_be_clickable(loc)).click()

    # 输入操作
    def base_input(self, loc, text):
        self.base_find_element(loc).send_keys(text)
